package cs.servlet;

import cs.utils.DBUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author qiuyihong
 * @description TODO
 * @date 2021/6/20 14:32
 */
@WebServlet(name = "addUser", urlPatterns = "/addUser")
public class AddUser extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");

        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String unit = request.getParameter("unit");
        String professional = request.getParameter("professional");
        String basicSalary = request.getParameter("basicSalary");
        String welfare = request.getParameter("welfare");
        String reward = request.getParameter("reward");
        String unemploymentInsurance = request.getParameter("unemploymentInsurance");
        String housingProvidentFund = request.getParameter("housingProvidentFund");
        String department = request.getParameter("department");

        System.out.println("接收参数:" + name + "," + sex + "," + age + "," + unit + "," + professional + "," + basicSalary + "," + welfare + "," + reward + "," + unemploymentInsurance + "," + housingProvidentFund + "," + department);

        int i = DBUtils.addUser(name, sex, age, unit, professional, basicSalary, welfare, reward, unemploymentInsurance, housingProvidentFund, department);

        if (i == 1) {
            request.setAttribute("msg", "操作成功");
        } else {
            request.setAttribute("msg", "操作失败");
        }
        request.getRequestDispatcher("/").forward(request, response);
    }
}
